package com.migu.spms.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.migu.spms.common.StringUtil;
import com.migu.spms.constant.Constant;
import com.migu.spms.dao.entity.SequenceCriteria;
import com.migu.spms.dao.entity.SequenceDO;
import com.migu.spms.dao.inf.ISequenceDO;
import com.migu.spms.service.inf.ISequenceCreateService;
import com.migu.spms.utils.CodeCreateUtil;
import com.migu.spms.utils.exception.ServiceException;

/**
 * 
* 项目名称:  SPMS测试设备
* 包:        com.migu.spms.service.impl.serviceUtil 
* 类名称:    SequeceServiceImpl.java
* 类描述:    序列号服务实现类
* 创建人:    fw  
* 创建时间:  2017年5月2日 下午1:34:45
 */
@Transactional
@Component("sequenceService")
public class SequenceServiceImpl implements ISequenceCreateService {
    private static Logger logger = LoggerFactory.getLogger(SequenceServiceImpl.class);
    @Resource(name = "ISequenceDO")
    private ISequenceDO sequeceDAO;

    @Override
    public String getRequireNumber() throws Exception {
        SequenceCriteria sequenceCriteria = new SequenceCriteria();
        sequenceCriteria.setSequenceType(Constant.SequenceType.REQUIRE_NUMBER);
        synchronized (this) {
            SequenceDO sequenceDO = sequeceDAO.selectByCriteria(sequenceCriteria);
            if (sequenceDO == null || StringUtil.isEmpty(sequenceDO.getSequence())) {
                logger.error(
                        CodeCreateUtil.createLogMessage("SequenceServiceImpl.getRequireNumber", "新建需求[查询需求序列号为空]"));
                throw new ServiceException("服务错误，请重试");
            }
            String sequence = sequenceDO.getSequence();
            StringBuffer sBuffer = new StringBuffer();
            long sequenceL = Long.valueOf(sequence);
            sequence = String.valueOf(sequenceL + 1);
            int addLength = 4 - sequence.length();
            sequence = (sBuffer.append(createZeroString(addLength)).append(sequence)).toString();
            SequenceCriteria sequenceCriteria4Update = new SequenceCriteria();
            sequenceCriteria4Update.setId(sequenceDO.getId());
            sequenceCriteria4Update.setSequence(sequence);
            sequeceDAO.updateByPrimaryKey(sequenceCriteria4Update);
            return sequence;
        }
    }

    private String createZeroString(int number) {
        StringBuffer sBuffer = new StringBuffer();
        while (number > 0) {
            sBuffer.append("0");
            number--;
        }
        return sBuffer.toString();
    }

    @Override
    public String getDeviceNumber(String deviceType) throws Exception {
        SequenceCriteria sequenceCriteria = new SequenceCriteria();
        sequenceCriteria.setSequenceType(deviceType);
        SequenceDO sequenceDO = sequeceDAO.selectByCriteria(sequenceCriteria);
        String sequence = sequenceDO.getSequence();
        if (sequenceDO == null || StringUtil.isEmpty(sequence)) {
            throw new ServiceException("服务错误，请重试");
        }
        StringBuffer sBuffer = new StringBuffer(sequence.substring(0, 4));
        sequence = sequence.substring(4);
        long sequenceL = Long.valueOf(sequence);
        sequence = String.valueOf(sequenceL + 1);
        int addLength = 6 - sequence.length();
        sequence = (sBuffer.append(createZeroString(addLength)).append(sequence)).toString();
        SequenceCriteria sequenceCriteria4Update = new SequenceCriteria();
        sequenceCriteria4Update.setId(sequenceDO.getId());
        sequenceCriteria4Update.setSequence(sequence);
        sequeceDAO.updateByPrimaryKey(sequenceCriteria4Update);
        return sequence;
    }
}
